var base_url_picstorage = '/rs/picstorage';
var base_url_pictype = '/rs/pictype';

var users = [];
var categories = [];
var operation = "add";
var currentPageNo = 1;
var pageRows = 10;

$(function() {
    authName = getCookie('username');

    queryCategories();

    $("#btnUserQuery").click(function() {
        currentPageNo = 1;
        queryList();
    });
});

function sendpic() {
    var content = $("#picfile").val();
    if(content.length > 0) {
        var form = document.getElementById("picform");
        var formData = new FormData(form);
        upajax('/upload', formData, function (result) {
            result = JSON.parse(result);
            if(result.success != null){
                $("#picShow").attr("src", result.success);
                $("#picPath").val(result.success);
                $("#picfile").val('');
            }
            else if (result.error != null)
                alert(result.error);
        });
    }
    else
        alert('请您选择需要上传的图片！');
}

function queryCategories() {
    zhget(base_url_pictype, {
    }, function(result) {
        var categoryList = $("#category");
        categories = result.rows;
        for (var al in categories) {
            var text = categories[al].name;
            var value = categories[al].id;
            categoryList.append("<option value='" + value + "'>" + text + "</option>");
        }
        queryList();
    });
}

function queryList() {
    zhget(base_url_picstorage, {
        title: $('#tisname').val(),
        sort: 'id',
        order: 'desc',
        page: currentPageNo,
        size: pageRows
    }, function(result) {
        users = result.rows;
        for(var i = 0; i<users.length; i++) {
            for(var j = 0 ; j < categories.length ; j++){
                if(result.rows[i]['type_id']==categories[j]["id"]){
                    result.rows[i]['category'] = categories[j]['name'];
                }
            }
        }
        buildTable(result, 'user-template', 'user-placeholder');
    });
}

function onUserAddClick() {
    cleanForm();
    operation = "add";
    $("#picId").attr("readonly", "readonly");
    $("#picPath").removeAttr("readonly");
    $('#userModal').modal('show');
}

function onUserUpdateClick(userid) {
    fillForm(userid);
    operation = "modify";
    $("#picId").attr("readonly", "readonly");
    $("#picPath").attr("readonly", "readonly");
    $('#userModal').modal('show');
}

function onDeleteClick(userid) {
    if(confirm("确认要删除？")) {
        zhdelete(base_url_picstorage + "/" + userid, {
            AUTHNAME:authName
        }, function (result) {
            if (result && result.affectedRows > 0) {
                queryList();
                showSuccess('删除成功！');
            } else {
                showError('删除失败！');
            }
        });
    }
}

function onUserSaveClick() {
    var picId = $("#picId").val();
    var data = {
        AUTHNAME:authName,
        title: $("#picName").val(),
        picbig: $("#picPath").val(),
        introduce: $("#introduce").val(),
        type_id: $("#category").val(),
    };
    if (operation == "add") {
        zhpost(base_url_picstorage, data, saveResult);
    } else {
        zhput(base_url_picstorage + "/" + picId, data, saveResult);
    }
}

function saveResult(result) {
    if (result) {
        if (result.outcome==0) {
            showError('保存失败！');
        } else{
            $('#userModal').modal('hide');
            queryList();
            showSuccess('保存成功！');
        }
    } else {
        showError('保存失败！');
    }
}

function fillForm(userid) {
    var index = 0;
    for (index in users) {
        var item = users[index];
        if (item.id == userid) {
            $("#picId").val(item.id);
            $("#picName").val(item.title);
            $("#picPath").val(item.picbig);
            $("#picShow").attr("src",item.picbig);
            $("#introduce").val(item.introduce);
            $("#category").val(item.type_id);
            return;
        }
    }
}

function cleanForm() {
    $("#picId").val("");
    $("#picName").val("");
    $("#picPath").val("");
    $("#introduce").val("");
    $("#picShow").attr("src","");
}
